﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.SYS
{
    public partial class _deptset2 : PageBase
    {
        public string whe = "", stype = "";
        public string us;
        public string tab = "sys_dept";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {
               // btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable d = BLL.Bllbase.get_table("sys_dept");
            ddldept.Items.Clear();
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddldept.Items.Add(new ExtAspNet.ListItem(d.Rows[i]["dept_nm"].ToString(), d.Rows[i]["dept_bh"].ToString()));
            }
            ddldept.DataBind();

            DataTable e = BLL.Bllbase.get_table("sys_user");
            ddlowen.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlowen.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString(), e.Rows[i]["sbh"].ToString()));
            }
            ddlowen.DataBind();
            ddlowen.Items.Add(new ExtAspNet.ListItem("",""));
            ddlowen.SelectedIndex = ddlowen.Items.IndexOf(ddlowen.Items.FindByValue(""));
        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {


            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row + val][0].ToString());
                sb.Append(",");

            }

            Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();


        }

        protected void btnDelete2_Click(object sender, EventArgs e)
        {


            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid2.SelectedRowIndexArray)
            {
                sb.Append(Grid2.DataKeys[row][0].ToString());
                sb.Append(",");

            }

            Alert.ShowInTop("你选择了菜单项：" + sb.ToString().TrimEnd(','));
          //  txt2.Text = sb.ToString().TrimEnd(',');
            BLL.Bllbase.DeleteList("sys_group", sb.ToString().TrimEnd(','));


        }

        private void BindGrid()
        {
            DataTable  table;

            string sqltxt = "SELECT * FROM "+tab+"";

            table =BLL.Bllbase.get_table(tab);
            Grid1.DataSource = table;
            Grid1.DataBind();

            table = BLL.Bllbase.get_table("sys_group");
            Grid2.DataSource = table;
            Grid2.DataBind();
        }

      

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table(tab);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }


        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.Bllbase.get_table("sys_group");
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }


        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
             string DD = Grid1.DataKeys[e.RowIndex][0].ToString();
             string bh = Grid1.DataKeys[e.RowIndex][1].ToString();
           if (e.CommandName == "sub")//填充条件 
            { 
              
                DataTable dt1 = BLL.Bllbase.get_table(tab, "sid=" + DD + "");           
                hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();
                txt1.Text = dt1.Rows[0]["dept_bh"].ToString().Trim();
                txt2.Text = dt1.Rows[0]["dept_nm"].ToString().Trim();
                
            }         
                DataTable dt = BLL.Bllbase.get_table("sys_group", "dbh='" + bh + "'");
           
                Grid2.DataSource = dt;
               Grid2.DataBind();

        }

        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            string DD = Grid2.DataKeys[e.RowIndex][0].ToString();
            string gname = Grid2.DataKeys[e.RowIndex][1].ToString();
           
            if (e.CommandName == "edit") //修改 
            {
          
            DataTable dt1 = BLL.Bllbase.get_table("sys_group", "sid=" + DD + "");
            hidsid2.Text = dt1.Rows[0]["sid"].ToString().Trim();         

            txtgname.Text = dt1.Rows[0]["gname"].ToString().Trim();

            ddldept.SelectedIndex = ddldept.Items.IndexOf(ddldept.Items.FindByValue(dt1.Rows[0]["dbh"].ToString()));
            ddlowen.SelectedIndex = ddlowen.Items.IndexOf(ddlowen.Items.FindByValue(dt1.Rows[0]["gobh"].ToString()));

            DataTable dt = BLL.Bllbase.get_table("sys_user", "dgroup='" + gname + "'");

            Grid3.DataSource = dt;
            Grid3.DataBind();
            }

        }


  
        protected void btsave_Click(object sender, EventArgs e)
        {
            string user = us;
            string t1 = txt1.Text.Trim();
            string t2 = txt2.Text.Trim();

            if (t1 == "" || t2 == "") return;


            string hid = hidsid.Text;
            string sqltxt = "";
            if (hid == "")
                sqltxt = "insert into " + tab + " values('" + t1 + "','" + t2 + "')";
            else
                sqltxt = "update " + tab + "  set dept_bh='" + t1 + "',dept_nm='" + t2 + "' where sid=" + hid;

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            hidsid.Text = "";

            txt1.Text = "";
            txt2.Text = "";

            BindGrid();

        }

        protected void btsave2_Click(object sender, EventArgs e)
        {
            string dbh = ddldept.SelectedValue;  //部门编号
            string dpart = ddldept.SelectedText;  //部门名称
            string gnm = txtgname.Text; //组名

            string gobh = ddlowen.SelectedValue; //组长编号
            string gowen = ddlowen.SelectedText;  //组长



            if (gnm == "") return;


            string hid = hidsid2.Text;
            string sqltxt = "";
            if (hid == "")
                sqltxt = "insert into sys_group values('" + dbh + "','" + dpart + "','" + gnm + "','" + gobh + "','" + gowen + "')";
            else
                sqltxt = "update sys_group  set dbh='" + dbh + "',dname='" + dpart + "',gname='" + gnm + "',gobh='" + gobh + "',goname='" + gowen + "' where sid=" + hid;

            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
            hidsid2.Text = "";

            txtgname.Text = "";
           // txt2.Text = "";

            BindGrid();
        }


        protected void Grid2_RowDataBound(object sender, GridRowEventArgs e)
        {
            //
           // ExtAspNet.CheckBox cb1 = Grid1.FindColumn("WindowField1") as ExtAspNet.WindowField;


        }





    }
}
